﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.EntityClient;
using System.Data.Common;
using System.Data;

namespace HardwareAs
{
    public partial class DownloadValid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DbDataReader reader = getDataSource();
            ListView1.DataSource = reader;
            ListView1.DataBind();



        }
        protected DbDataReader getDataSource() //Init 
        {
            HardwareEntities1 hdd = new HardwareEntities1();
            EntityConnection conn = (EntityConnection)hdd.Connection;
            DbConnection dc = conn.StoreConnection;
            DbCommand dcomm = dc.CreateCommand();
            dcomm.CommandText = "SelectInvalidDownload";
            dcomm.CommandType = CommandType.StoredProcedure;
            bool openConnection = dcomm.Connection.State ==
                ConnectionState.Closed;

            if (openConnection)
            {
                dcomm.Connection.Open();
            }
            var reader = dcomm.ExecuteReader();
            return reader;
        }

        protected void Approve(object sender, EventArgs e) //Approve Event
        {
            HardwareEntities1 hdd = new HardwareEntities1();
            EntityConnection conn = (EntityConnection)hdd.Connection;
            DbConnection dc = conn.StoreConnection;
            DbCommand dcomm = dc.CreateCommand();
            dcomm.CommandText = "UpdateValidDownload";
            dcomm.CommandType = CommandType.StoredProcedure;
            DbParameter p1 = dcomm.CreateParameter();
            p1.ParameterName = "@Id";
            p1.DbType = DbType.Int32;
            p1.Value = (Convert.ToInt32((sender as Button).CommandArgument));
            dcomm.Parameters.Add(p1);
            DbParameter p2 = dcomm.CreateParameter();
            p2.ParameterName = "@valid";
            p2.DbType = DbType.Boolean;
            p2.Value = true;
            dcomm.Parameters.Add(p2);
            bool openConnection = dcomm.Connection.State ==
               ConnectionState.Closed;

            if (openConnection)
            {
                dcomm.Connection.Open();
            }
            dcomm.ExecuteNonQuery();
            DbDataReader reader = getDataSource();
            ListView1.DataSource = reader;
            ListView1.DataBind();

        }

        protected void Disapprove(object sender, EventArgs e) //Disapprove event
        {
            HardwareEntities1 hdd = new HardwareEntities1();
            EntityConnection conn = (EntityConnection)hdd.Connection;
            DbConnection dc = conn.StoreConnection;
            DbCommand dcomm = dc.CreateCommand();
            dcomm.CommandText = "UpdateValidDownload";
            dcomm.CommandType = CommandType.StoredProcedure;
            DbParameter p1 = dcomm.CreateParameter();
            p1.ParameterName = "@Id";
            p1.DbType = DbType.Int32;
            p1.Value = (Convert.ToInt32((sender as Button).CommandArgument));
            dcomm.Parameters.Add(p1);
            DbParameter p2 = dcomm.CreateParameter();
            p2.ParameterName = "@valid";
            p2.DbType = DbType.Boolean;
            p2.Value = false;
            dcomm.Parameters.Add(p2);
            bool openConnection = dcomm.Connection.State ==
               ConnectionState.Closed;

            if (openConnection)
            {
                dcomm.Connection.Open();
            }
            dcomm.ExecuteNonQuery();
            DbDataReader reader = getDataSource();
            ListView1.DataSource = reader;
            ListView1.DataBind();

        }
}
}